package com.codes.core.utils.word;

import com.codes.core.utils.snowflake.SnowUtil;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.NumbericRenderData;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.util.BytePictureUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/** @deprecated */
/**
 * @Description   poi-tl  word文档 模板 动态 导出word
 *                  不用转换 直接使用 占位符 {{title}}
 *                   暂时使用 wordUtils
 *
 * test-commit
 *
 * @Date 2020/7/2 9:24
 * @Created by peanut
 */
@Slf4j
//@Component
public class PoiTlUtils {


    @Value("${file-save-temp-path}")
    private   String fileSaveTempPath;
    // 注入静态变量
    private static String fileTemp;

   @PostConstruct
    public void init(){
        fileTemp = fileSaveTempPath;
    }

    private static final String templateFolder = PoiTlUtils.class.getResource("/template/word/").getPath();


    public static File getWordDoc(String tplUrl, String tplName,Map<String,Object> mapParam) throws IOException {

        String fileName = SnowUtil.getOne().nextId() + ""; // 雪花 id 生成
        // 文件生成的路径
        String reUrl = fileTemp + fileName+".docx";
        File re = new File(reUrl);
        tplUrl = PoiTlUtils.class.getResource(tplUrl).getPath() + tplName;
        log.info("word模板路径:{}",tplUrl);
        log.info("自动生成word文件路径:{}",reUrl);
        XWPFTemplate template = XWPFTemplate.compile(tplUrl)    // 模板路径
                .render(mapParam);// 渲染参数
        FileOutputStream out = new FileOutputStream(reUrl);
        template.write(out);
        out.flush();
        out.close();
        template.close();
        return re;
    }

    // 测试用的
    public static File getWord() throws IOException {

        String fileName = SnowUtil.getOne().nextId() + "";

        String reUrl = "D:\\dev_data\\word\\22222222\\"+ fileName+".docx";
        long id =  1;//SnowUtil.parseId(1222L);
        File re = new File(reUrl);
        String tplName = "站点信息检测表遮挡车牌.docx";
        String tplUrl = fileTemp + tplName;
        File ff = new File(tplUrl);
        System.out.println(tplUrl);
        XWPFTemplate template = XWPFTemplate.compile(tplUrl)
                .render(  new HashMap<String, Object>(){{
                        //
                        put("title", "poi-tl Word模板引擎");
                        put("chepai", "豫A 88888");
                             // 本地图片                        宽           高           uri
                        put("image001", new PictureRenderData(555, 400, "D:\\dev_data\\images\\222.jpg"));
                        put("image002", new PictureRenderData(500, 400, "D:\\dev_data\\images\\333.jpg"));
                        // 图片流
                        put("localbyte", new PictureRenderData(80, 100, ".png"
                                , new FileInputStream("./logo.png")));
                        // 网络图片
                     /*   put("urlpicture", new PictureRenderData(50, 50, ".png"
                                , BytePictureUtils.getUrlBufferedImage("http://deepoove.com/images/icecream.png")));*/
                        // java 图片
                        // put("bufferimage", new PictureRenderData(80, 100, ".png", bufferImage)));

                        put("list", new NumbericRenderData(new ArrayList<TextRenderData>() {
                            {
                                add(new TextRenderData("Plug-in function, define your own function"));
                                add(new TextRenderData("Supports word text, header..."));
                                add(new TextRenderData("Not just templates, but also style templates"));
                            }
                         }));



                        }});
        FileOutputStream out = new FileOutputStream(reUrl);
        template.write(out);
        out.flush();
        out.close();
        template.close();
        return re;
    }

    // 返回 文件流  文件乱码 ,此方法禁用
    public static void getWord2(HttpServletResponse response) throws IOException {
        String reUrl = "output.docx";
   //     File re = new File(reUrl);
        String tplUrl = templateFolder + "11.docx";
     //   File ff = new File(tplUrl);
        System.out.println(tplUrl);
        XWPFTemplate template = XWPFTemplate.compile(tplUrl)
                .render(
                        new HashMap<String, Object>(){{
                             put("title", "poi-tl Word模板引擎");
                        }});
        String  fileName = "test.docx";
        response.reset();
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //获取oss端文件的文件名，用作于下载文件的名称
        response.setContentType("application/octet-stream");
        //处理文件名为中文的情况
        response.setHeader("Content-Disposition", "attach;filename="
                + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        ServletOutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        template.close();
        out.flush();
        out.close();
    }



}